//
//  CXDestinationGoodViewController.m
//  Travel
//
//  Created by dllo on 15/11/30.
//  Copyright © 2015年 doll. All rights reserved.
//

#import "CXDestinationGoodViewController.h"
#import "CXNetHandle.h"
#import "CXDestinationGoodModel.h"
#import "CXDestinationDataModel.h"
#import "CXDestinationDetailGoodTableViewCell.h"
#import "KHHTravelNotesDetailPageViewController.h"

@interface CXDestinationGoodViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArr;

@end

@implementation CXDestinationGoodViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self addScreenEdgePan];
    self.view.backgroundColor = [UIColor colorWithRed:0.98 green:0.96 blue:0.91 alpha:1];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"leftBack"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(backToLast)];
    
    [self getData];
    [self createTableView];
    [CXLoadMBProgress loadMBProgress:self.view];
}


#pragma mark - 获取数据
- (void)getData
{
    NSString *url = [NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/%@/%@/trips/", [self.model_good.typeId stringValue], self.model_good.valueId];
    [CXNetHandle getUrl:url cookie:nil result:^(id result) {
        
        [CXLoadMBProgress removeMBProgress:self.view];
        self.dataArr = [CXDestinationGoodModel transformWithArray:result[@"items"]];
        
        [self.tableView reloadData];
        
            }];
    
}


#pragma mark - 创建tableView
- (void)createTableView
{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[CXDestinationDetailGoodTableViewCell class] forCellReuseIdentifier:@"cell_good"];
}
#pragma mark - 自定义按钮触发方法
- (void)backToLast
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CXDestinationDetailGoodTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell_good"];
    cell.model_good = self.dataArr[indexPath.row];
    cell.backgroundColor = [UIColor colorWithRed:0.98 green:0.96 blue:0.91 alpha:1];
    return cell;
}


#pragma mark - tableView点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    KHHTravelNotesDetailPageViewController *note = [[KHHTravelNotesDetailPageViewController alloc] init];
    note.ForthID = [self.dataArr[indexPath.row] Iid];
    note.tagtag = 10;
    
    [self.navigationController pushViewController:note animated:YES];
   

    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return WIDTH / 3 + 20 + 20;
}
#pragma mark-滑动手势1
- (void)addScreenEdgePan
{
    UIScreenEdgePanGestureRecognizer *screenEdgePan = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(presentLeftMenuViewController:)];
    screenEdgePan.edges = UIRectEdgeLeft;
    [self.view addGestureRecognizer:screenEdgePan];
    [self.tableView addGestureRecognizer:screenEdgePan];
}
#pragma mark-滑动手势2
-(void)presentLeftMenuViewController:(UIScreenEdgePanGestureRecognizer *)edge
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
